﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using ExtremeML.Core;
using ExtremeML.Packaging;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class ColumnsWrapperCollection
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing <see cref="WorkbookPartWrapper"/>.
		/// </summary>
		public WorkbookPartWrapper WorkbookPart
		{
			get { return Worksheet.WorksheetPart.WorkbookPart; }
		}
	
		/// <summary>
		/// Gets the containing <see cref="WorksheetWrapper"/>.
		/// </summary>
		public WorksheetWrapper Worksheet
		{
			get { return (WorksheetWrapper)Parent; }
		}

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns the <see cref="ColumnWrapper"/> for the column having the specified index.
		/// </summary>
		/// <param name="columnIndex">The zero-based index of the target column.</param>
		/// <returns>A <see cref="ColumnWrapper"/> that matches the specified index. <c>null</c> if no match is found.</returns>
		internal ColumnWrapper Find(
			int columnIndex)
		{
			if (Items.Count > 0)
			{
//ToDo: review whether Items.Count > 1 is possible.
				return Items[0].Columns.Find(columnIndex);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Returns the visible width (in pixels) of the column having the specified index.
		/// </summary>
		/// <param name="columnIndex">The zero-based index of the target column.</param>
		/// <returns>An <see cref="int"/> that specifies the visible width of the specified column.</returns>
		internal int GetVisibleWidth(
			int columnIndex)
		{
			var digitWidth = WorkbookPart.WorkbookStylesPart.Stylesheet.GetNormalFont().MaximumDigitWidth;
			var column = Find(columnIndex);

			if (column == null)
			{
				// When no explicit column definition exists we use default values.
				return (int)Math.Round(ColumnWrapper.GetDefaultColumnWidth() * digitWidth);
			}
			else
			{
				// Use the explicitly defined width .
				if (column.Hidden.HasValue && column.Hidden.Value)
				{
					// Hidden columns (explicit hiding or collapsed outlining) have zero width.
					return 0;
				}
				else if (!column.CustomWidth.HasValue || !column.CustomWidth.Value)
				{
					// This is an explicit column definition with default width.
					return (int)Math.Round(ColumnWrapper.GetDefaultColumnWidth() * digitWidth);
				}
				else
				{
					// Visible column width is calculated using the OpenXML specification formula.
					if (column.Width.HasValue)
					{
						return (int)Math.Round(column.Width.Value * digitWidth);
					}
					else
					{
						throw new PackageException("Underlying package element definition is incomplete.");
					}
				}
			}
		}

		#endregion
		#region Overrides


		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods


		#endregion
	}
}
